package com.cd.system.jpa.repository;

import com.cd.common.domain.SysMenu;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository("SysMenuRepository")
@Transactional
public interface SysMenuRepository extends CrudRepository<SysMenu,Long> {

    public List<SysMenu> findAll();

    @Query(value = "select sysMenu from SysMenu sysMenu where sysMenu.menuType in ('M', 'C') and sysMenu.status ='0'" +
            "order by sysMenu.parentId, sysMenu.orderNum")
    public List<SysMenu> selectMenuTreeAll();


    /**
     * 根据用户ID查询菜单
     *
     * @param userId 用户名称
     * @return 菜单列表
     */
    @Query(value = "\t\t\t\tselect distinct m.*\n" +
            "\t\tfrom sys_menu m\n" +
            "\t\t\t left join sys_role_menu rm on m.menu_id = rm.menu_id\n" +
            "\t\t\t left join sys_user_role ur on rm.role_id = ur.role_id\n" +
            "\t\t\t left join sys_role ro on ur.role_id = ro.role_id\n" +
            "\t\t\t left join sys_user u on ur.user_id = u.user_id\n" +
            "\t\twhere u.user_id = ?1 and m.menu_type in ('M', 'C') and m.status = 0  AND ro.status = 0\n" +
            "\t\torder by m.parent_id, m.order_num",nativeQuery = true)
    List<SysMenu> selectMenuTreeByUserId(Long userId);
}
